package com.hrsoft.modules.sys.service.impl;

import com.hrsoft.common.utils.PageUtils;
import com.hrsoft.common.utils.Query;
import com.hrsoft.modules.sys.service.SysItemdetailsService;
import com.hrsoft.modules.sys.dao.SysItemsDao;
import com.hrsoft.modules.sys.entity.SysItemsEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.hrsoft.modules.sys.dao.SysItemdetailsDao;
import com.hrsoft.modules.sys.entity.SysItemdetailsEntity;


@Service("sysItemdetailsService")
public class SysItemdetailsServiceImpl extends ServiceImpl<SysItemdetailsDao, SysItemdetailsEntity> implements SysItemdetailsService {

    @Autowired
    private SysItemsDao sysItemsDao;

    @Autowired
    private SysItemdetailsDao sysItemdetailsDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<SysItemdetailsEntity> detailWrapper = new QueryWrapper<>();
        if (params.get("categoryId") != null ) {
            detailWrapper.eq("category_id", params.get("categoryId"));
        }
        if (params.get("key") != null && !params.get("key").equals("") ) {
            detailWrapper.and(i -> i.like("item_code", params.get("key")).or().like("item_name", params.get("key")));
        }
        detailWrapper.orderByAsc("sort_code", "id");
        IPage<SysItemdetailsEntity> page = this.page(
                new Query<SysItemdetailsEntity>().getPage(params),
                detailWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<SysItemdetailsEntity> getListByCode(String code) {
        QueryWrapper<SysItemsEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("code", code);
        SysItemsEntity items = sysItemsDao.selectOne(wrapper);

        QueryWrapper<SysItemdetailsEntity> detailWrapper = new QueryWrapper<>();
        detailWrapper.eq("category_id", items.getId());
        detailWrapper.eq("enabled", 1);
        detailWrapper.orderByAsc("sort_code", "id");
        return sysItemdetailsDao.selectList(detailWrapper);
    }

}